package com.example.`2022110341`

import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.`2022110341`.weather.Forecast
import com.example.`2022110341`.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {

    val baseURL="http://t.weather.itboy.net/api/weather/city/"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        val city: TextView=findViewById(R.id.city)
        val province: TextView=findViewById(R.id.province)
        val t: TextView=findViewById(R.id.T)
        val humidity: TextView=findViewById(R.id.humidity)
        val image: ImageView=findViewById(R.id.weather)
        val list: ListView=findViewById(R.id.list)
        val cityCode=intent.getStringExtra("city_code")
        val queue= Volley.newRequestQueue(this)
        val stringRequest= StringRequest(baseURL+cityCode,{
            val gson= Gson()
            val weatherType=object : TypeToken<Weather>(){}.type
            val weather=gson.fromJson<Weather>(it,weatherType)
            city.text=weather.cityInfo.city
            province.text=weather.cityInfo.parent
            t.text=weather.data.wendu+"℃"
            humidity.text="湿度:"+weather.data.shidu


            val today=weather.data.forecast.first()
            when(today.type){
                "晴"->image.setImageResource(R.drawable.sun)
                "阴"->image.setImageResource(R.drawable.cloud)
                "多云"->image.setImageResource(R.drawable.mcloud)
                "小雨"->image.setImageResource(R.drawable.rain)
                else -> image.setImageResource(R.drawable.thunder)
            }
            val adapter= ArrayAdapter<Forecast>(this,android.R.layout.simple_list_item_1,weather.data.forecast)
            list.adapter=adapter

            Log.d("Main2ERRORFalse","${weather.cityInfo.city}${weather.cityInfo.parent}")
        },{
            Log.d("Main2ERROR","$it")
        })
        queue.add(stringRequest)
    }
}